/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package speakerphonesensor;

import Communication.Communicator;
import Wasa.Response;
import Wasa.Type;

/**
 *
 * @author gianmariospacagna
 */
class RealTimeMeasurement extends Thread {

    private SpeakerPhoneSensor sps;
    private CircularArray temp;
    private CircularArray light;
    private Communicator c;
    boolean active;

    public RealTimeMeasurement(SpeakerPhoneSensor sps, Communicator c, CircularArray light, CircularArray temp) {
        this.sps = sps;
        this.c = c;
        this.light = light;
        this.temp = temp;
        this.active = false;
    }

    @Override
    public void run() {

        Response resp;
        while (true) {
            
            if (active) {
                resp = c.measureLight();
                if (resp.getType().compareTo(Type.DATA) == 0) {
                    light.addElement(resp.getValue());
                    float var = light.getVariance();
                    float dev= (float) Math.sqrt(var);
                    float avg = light.getAverage();
                    float slope = light.getSlope();
                    float threshold= dev*(1-slope/avg);
                    //System.out.println(light.getN()+";"+resp.getValue()+";"+avg+";\t"+dev);
                    
                    if (dev > 800) {
                        float left=light.getFirstHalfAverage();
                        float right=light.getSecondHalfAverage();
                        //System.out.println("Event happened, dev: "+dev+" left: "+left+" right: "+right);
                        if (left<right) {
                            
                            sps.activeEarPiece();
                        } else {
                            sps.activateSpeakerPhone();
                        }

                    }
                }
                
            }
        }

    }

    public void activate() {
        active = true;
    }

    public void standbye() {
        active = false;
    }

    public CircularArray getLight() {

        return this.light;
    }

    public CircularArray getTemp() {

        return this.temp;
    }

    public void calibrate() {
        calibrateTemp();
        calibrateLight();

    }

    private void calibrateTemp() {

        while (!this.temp.isFull()) {
            Response resp = c.measureTemp();
            System.out.println("Measure T: " + resp.getType() + resp.getValue());
            if (resp.getType().compareTo(Type.DATA) == 0) {
                this.temp.addElement(resp.getValue());
            }
        }

    }

    private void calibrateLight() {
        while (!this.light.isFull()) {
            Response resp = c.measureLight();
            System.out.println("Measure L: " + resp.getType() + resp.getValue());
            if (resp.getType().compareTo(Type.DATA) == 0) {
                this.light.addElement(resp.getValue());
            }
        }
    }
}